package engine.input;

import org.lwjgl.input.Keyboard;
import util.Util;

public class Keys 
{
    
    public static int // Keyboard indices, of type: NAME_DESC
            Q, W, E, R, T, Y, U, I, O, P,
            A, S, D, F, G, H, J, K, L,
            Z, X, C, V, B, N, M,
            ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, ZERO,
            F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12,
            ESCAPE, TAB, ENTER, CTRL_L, SHIFT_L, SHIFT_R, SPACE, ENTER_NUMPAD, CTRL_R,
            UP, LEFT, DOWN, RIGHT;
    
    
    private boolean[] keys;
    private boolean[] keysPrev;
    
    public Keys() 
    {
        keys = new boolean[Keyboard.KEYBOARD_SIZE];
        keysPrev = new boolean[Keyboard.KEYBOARD_SIZE];
        Keyboard.enableRepeatEvents(true);
        try 
        {
            Keyboard.create();
        }
        catch (Exception e) 
        {
            Util.console(e);
            Util.quit();
        }
        initIndices();
    }
    
    public void update() // Update keyboard input array
    {
        for (int i = 0; i < keys.length; i++) 
        {
            boolean isDown = Keyboard.isKeyDown(i);
            keysPrev[i] = keys[i];
            keys[i] = isDown;
        }
    }

    public boolean isDown(int key) 
    {
        return keys[key];
    }
    
    public boolean wasPressed(int key) 
    {
        return (keys[key] && !keysPrev[key]);
    }
    
    public boolean wasReleased(int key) 
    {
        return (!keys[key] && keysPrev[key]);
    }
    
    public void destroy() 
    {
        keys = null;
        keysPrev = null;
        Keyboard.destroy();
    }
    
    private void initIndices()  // Called on construction
    {
        int i0 = 16;
        int i1 = 30;
        int i2 = 44;
        int i3 = 2;
        int i4 = 59;
        int i5 = 87;
        Q = i0++;
        W = i0++;
        E = i0++;
        R = i0++;
        T = i0++;
        Y = i0++;
        U = i0++;
        I = i0++;
        O = i0++;
        P = i0++;
        A = i1++;
        S = i1++;
        D = i1++;
        F = i1++;
        G = i1++;
        H = i1++;
        J = i1++;
        K = i1++;
        L = i1++;
        Z = i2++;
        X = i2++;
        C = i2++;
        V = i2++;
        B = i2++;
        N = i2++;
        M = i2++;
        ONE = i3++;
        TWO = i3++;
        THREE = i3++;
        FOUR = i3++;
        FIVE = i3++;
        SIX = i3++;
        SEVEN = i3++;
        EIGHT = i3++;
        NINE = i3++;
        ZERO = i3++;
        F1 = i4++;
        F2 = i4++;
        F3 = i4++;
        F4 = i4++;
        F5 = i4++;
        F6 = i4++;
        F7 = i4++;
        F8 = i4++;
        F9 = i4++;
        F10 = i4++;
        F11 = i5++;
        F12 = i5++;
        ESCAPE = 1;
        TAB = 15;
        ENTER = 28;
        CTRL_L = 29;
        SHIFT_L = 42;
        SHIFT_R = 54;
        SPACE = 57;
        ENTER_NUMPAD = 156;
        CTRL_R = 157;
        UP = 200;
        LEFT = 203;
        RIGHT = 205;
        DOWN = 208;
    }
    
}
